package cn.thoughtworks.school.response;

import cn.thoughtworks.school.entities.Video;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;

import java.util.Date;
import java.util.Map;

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class VideoResponse {
  private Long id;
  private String coverUrl;
  private String url;
  private String title;
  private String description;
  private Boolean isAllVisible;
  private Boolean isPublic;
  private Boolean isRelease;
  private String username;
  private Long serialId;
  private Boolean isNeedSubscribe;
  @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  private Date createTime;

  public static VideoResponse build(Map user, Video video, boolean isCurrentUserNeedSubscribe) {
    if (isCurrentUserNeedSubscribe) {
      video.setUrl("");
    }
    return VideoResponse.builder()
      .id(video.getId())
      .serialId(video.getSerialId())
      .coverUrl(video.getCoverUrl())
      .url(video.getUrl())
      .title(video.getTitle())
      .description(video.getDescription())
      .isAllVisible(video.getIsAllVisible())
      .isPublic(video.getIsPublic())
      .isRelease(video.getIsRelease())
      .createTime(video.getCreateTime())
      .username(user.get("name").toString())
      .isNeedSubscribe(isCurrentUserNeedSubscribe)
      .build();
  }

  public static VideoResponse build(Map user, Video video) {
    return VideoResponse.builder()
      .id(video.getId())
      .serialId(video.getSerialId())
      .coverUrl(video.getCoverUrl())
      .url(video.getUrl())
      .title(video.getTitle())
      .description(video.getDescription())
      .isAllVisible(video.getIsAllVisible())
      .isPublic(video.getIsPublic())
      .isRelease(video.getIsRelease())
      .createTime(video.getCreateTime())
      .username(user.get("name").toString())
      .isNeedSubscribe(false)
      .build();
  }
}
